package live.ifox.stage;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.ID;
import live.ifox.factory.Url;
import live.ifox.helper.CommonHelper;

public class AssembleLogEditor extends BaseRestActivity {
    boolean updateFlag;
    Spinner sp_providers, sp_names;
    List<String> providers, names;
    EditText in_provider, in_name, in_number, in_remark;

    int _id;
    String _provider, _name, _remark;
    int _number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assemble_log_editor);
        actionBar.setTitle(R.string.assemble_log);

        updateFlag = false;
        entry = Entry.AssembleLog;
        baseUrl = Url.AssembleLog;

        sp_providers = findViewById(R.id.providers);
        sp_names = findViewById(R.id.names);
        in_provider = findViewById(R.id.provider);
        in_name = findViewById(R.id.name);
        in_number = findViewById(R.id.number);
        in_remark = findViewById(R.id.remark);

        sp_providers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                in_provider.setText(providers.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        sp_names.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                in_name.setText(names.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        _id = getIntent().getIntExtra("id", 0);
        if (_id > 0) {
            updateFlag = true;
            _provider = getIntent().getStringExtra("provider");
            _name = getIntent().getStringExtra("name");
            _number = getIntent().getIntExtra("number", 0);
            _remark = getIntent().getStringExtra("remark");

            if (_number != 0)
                in_number.setText(String.valueOf(_number));

            if (!_remark.isEmpty())
                in_remark.setText(_remark);
        }
        getInfo();
    }

    void getInfo() {
        httpGet(baseUrl + "get_info");
    }

    public void onClean_Click(View view) {
        in_provider.setText("");
        in_name.setText("");
        in_number.setText("");
        in_remark.setText("");
    }

    public void onSubmit_Click(View view) {
        String provider = in_provider.getText().toString();
        String name = in_name.getText().toString();
        String numberStr = in_number.getText().toString();
        if (provider.isEmpty() || name.isEmpty() ||
                numberStr.isEmpty()) {
            CommonHelper.showMessage(this, "输入不能为空！");
            return;
        }

        int number = 0;
        try {
            number = Integer.parseInt(numberStr);
        } catch (NumberFormatException e) {
            CommonHelper.showMessage(this, "输入格式不正确！");
            return;
        }
        String remark = in_remark.getText().toString();
        Map<String, Object> map = new HashMap<>();
        if (!updateFlag) {
            map.put("provider", provider);
            map.put("name", name);
            map.put("number", number);
            map.put("remark", remark);
            httpPost(baseUrl + "insert", JSON.toJSONString(map));
        } else {
            if (!provider.equals(_provider))
                map.put("provider", provider);

            if (!name.equals(_name))
                map.put("name", name);

            if (number != _number)
                map.put("number", number);

            if (!remark.equals(_remark))
                map.put("remark", remark);

            if (map.isEmpty())
                return;

            map.put("id", _id);
            httpPost(baseUrl + "update", JSON.toJSONString(map));
        }
        setResult(ID.RESULT_DONE);
        finish();
    }

    @Override
    public void processReply(JSONObject rootObject) {
        if (!rootObject.containsKey("entry") ||
                !rootObject.getString("entry").equals(entry))
            return;

        if (rootObject.containsKey("providers")) {
            JSONArray array = rootObject.getJSONArray("providers");
            if (array.isEmpty())
                return;

            if (!providers.isEmpty())
                providers.clear();

            providers = array.toJavaList(String.class);
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_spinner_dropdown_item);
            adapter.addAll(providers);
            sp_providers.setAdapter(adapter);
            if (updateFlag)
                if (providers.contains(_provider))
                    sp_providers.setSelection(providers.indexOf(_provider));
        }

        if (rootObject.containsKey("names")) {
            JSONArray array = rootObject.getJSONArray("names");
            if (array.isEmpty())
                return;

            if (!names.isEmpty())
                names.clear();

            names = array.toJavaList(String.class);
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_spinner_dropdown_item);
            adapter.addAll(names);
            sp_names.setAdapter(adapter);
            if (updateFlag)
                if (names.contains(_name))
                    sp_names.setSelection(names.indexOf(_name));
        }
    }
}
